package ${package};

import jakarta.annotation.Resource;
import org.jl.demo.dao.${baseRequestMapping}.${className}PagingRepository;
import org.jl.demo.dao.${baseRequestMapping}.${className}Repository;
import org.jl.demo.model.${baseRequestMapping}.${className};
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class ${className}Service {

    @Resource
    private ${className}Repository ${baseRequestMapping}Repository;

    @Resource
    private ${className}PagingRepository ${baseRequestMapping}PagingRepository;

    public Page<${className}> page(Pageable pageable) {
        return ${baseRequestMapping}PagingRepository.findAll(pageable);
    }

    public Optional<${className}> getById(Long id) {
        return ${baseRequestMapping}Repository.findById(id);
    }

    public void insert(${className} ${baseRequestMapping}) {
        ${baseRequestMapping}Repository.save(${baseRequestMapping});
    }

    public void update(Long id, ${className} ${baseRequestMapping}Detail) {
        ${className} ${baseRequestMapping} = ${baseRequestMapping}Repository.findById(id).orElseThrow(() -> new RuntimeException("${className} not found"));
        BeanUtils.copyProperties(${baseRequestMapping}Detail, ${baseRequestMapping}, "id");
        ${baseRequestMapping}Repository.save(${baseRequestMapping});
    }

    public void delete(Long id) {
        ${baseRequestMapping}Repository.deleteById(id);
    }
}